<?php
/**
 * @Author:      sqq
 * @DateTime:    2018-07-09 12:46:57
 * @Description: 用于维护系统资源 的 控制器
 */
class AhpResourceController extends BaseController
{
    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 14:16:10
     * @Description: 用于展示资源的列表页
     * @link: http://www.activity.com/ahp/ahp3.0/resource/index?token=36e1c74f6aa85a2b8bf1a7796e93a0e0&source=1
     */
    public function index()
    {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'source' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }
            $resource_arr=array();
            $resource=AhpResource::where('type',1)->where('source',Input::get('source'))->whereIn('status',array(1,2))->orderBy('sort','asc')->get();//获取所有能用的资源类
            foreach ($resource as $_r) {
                $_r['icons'] = $_r['icon'];
                unset($_r['icon']);
                $r_s=AhpResource::where('pid',$_r->id)->whereIn('status',array(1,2))->orderBy('sort','asc')->get();
                $rs_arr=array();
                foreach ($r_s as $_s) {
                    $rs_arr[]=$_s;
                }
                $_r['s_r']=$rs_arr;
                $resource_arr[]=$_r;
            }
            $response = array('code' => 0, 'msg' => 'ok','result' => array('resource' => $resource_arr));
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "resource/index", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }



    /**
     * @Author:      sqq
     * @DateTime:    2016-07-09 14:17:16
     * @Description: 用于修改 资源的信息
     * @link: http://www.activity.com/ahp/ahp3.0/resource/store?token=36e1c74f6aa85a2b8bf1a7796e93a0e0&name=创建角色&pid=0&type=2&source=1
     */
    public function store()
    {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'name' => 'required',
                'pid' => 'required',
                'type' => 'required',
                'source' => 'required', //1-ssp 2-arc
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }
            $resource=AhpResource::find(Input::get('id'));
            if(!$resource){//如果不存在说明是 新增
                $resource=new AhpResource;
                $resource->status=1;
            }
            $resource->url=Input::get("url","");
            $resource->name=Input::get("name");
            $resource->pid=Input::get("pid");
            $resource->type=Input::get("type");
            $resource->sort=Input::get("sort");
            $resource->source=Input::get("source");
            $resource->icon=Input::get("icons");
            $resource->en_name=Input::get("en_name");
            $resource->save();

            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "resource/store", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 18:52:39
     * @Description: 修改资源状态
     * @link: http://www.activity.com/ahp/ahp3.0/resource/status?token=28859210b692656c3af38e265e93635b&ids=1&status=1
     */
    public function status()
    {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ids' => 'required',
                'status' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }
            $ids = Input::get('ids');
            $arr_id = explode(',',rtrim($ids,','));
            if(count($arr_id)){
                foreach($arr_id as $_a){
                    $resource=AhpResource::find($_a);
                    $resource->status=Input::get("status");
                    $resource->updated_at = date('Y-m-d H:i:s', time());
                    $resource->save();
                }
            }
            $response = array('code' => 0, 'msg' => 'ok');
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "resource/status", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 17:48:29
     * @Description: 资源详情  也可以用作资源编辑页
     * @link: http://www.activity.com/ahp/ahp3.0/resource/show?token=28859210b692656c3af38e265e93635b&id=1
     */
    public function show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ssp_sys_user=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ssp_sys_user)){
                break;
            }
            $resource=AhpResource::find(Input::get('id'));
            $response = array('code' => 0, 'msg' => 'ok','result' => $resource);
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "resource/show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}